package com.sugar.web.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sugar.base.util.Const;

/**
 * @标题：过滤未登录用户限制访问后台页面
 * @描述：拦截非验证者直接访问JSP页面
 * @编写者：杨浩泉
 * @日期：2011-10-17 <b>下午08:51:23</b>
 */
public class PageAccessPrivilegeFilter implements Filter{
	
	private Log log = LogFactory.getLog(this.getClass()) ;
	
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req ;
		HttpServletResponse response = (HttpServletResponse) resp;
		
		if(null != request.getSession().getAttribute(Const.USER_SESSION_KEY)) {
			log.info("[" + request.getRemoteAddr() + "]访问-" + request.getRequestURI());
			chain.doFilter(request, response) ;
		} else {
			log.info("[" + request.getRemoteAddr() + "]" + "非法访问-"+request.getRequestURI()) ;
			response.sendRedirect(request.getContextPath()+"/web_manager/login.jsp") ;
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
	}
	
	@Override
	public void destroy() {}
	
	

}
